package com.qcxk.yuqian.system.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qcxk.yuqian.system.component.behaviour.BehaviourOperator;
import com.qcxk.yuqian.system.component.constant.SystemConstant;
import com.qcxk.yuqian.system.entity.Organ;
import com.qcxk.yuqian.system.service.OrganService;
import com.qcxk.yuqian.system.utils.ThmsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/organ")
public class OrganController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private OrganService organService;
    @BehaviourOperator(desc = "新增机构")
    @RequestMapping(value = "/addOrgan")
    public JSONObject addOrgan(Organ organ) {
        JSONObject json = new JSONObject();
        organ.setCtime(ThmsUtil.getDate());
        if (organService.save(organ)) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }
    @BehaviourOperator(desc = "修改机构")
    @RequestMapping(value = "/updateOrgan")
    public JSONObject updateOrgan(Organ organ) {
        JSONObject json = new JSONObject();
        organ.setUtime(ThmsUtil.getDate());
        if (organService.updateOrgan(organ)>0) {
            json.put("code", "0");
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }
    @BehaviourOperator(desc = "查询机构信息")
    @RequestMapping(value = "/loadOrgan")
    public JSONObject loadOrgan(Organ organ) {
        JSONObject json = new JSONObject();
        organ = organService.getById(organ.getId());
        if (organ != null) {
            json.put("code", "0");
            json.put("data", organ);
        } else {
            json.put("code", "1");
            json.put("msg", SystemConstant.SYS_DATABASE_ERROR_CN);
        }
        return json;
    }

    @BehaviourOperator(desc = "查询机构列表")
    @RequestMapping(value = "/queryOrganList")
    public JSONObject queryOrganList(Page page, Organ organ) {
        JSONObject json = new JSONObject();
        page = organService.queryOrganList(page, organ);
        json.put("code", "0");
        json.put("current", page.getCurrent());
        json.put("size", page.getSize());
        json.put("total", page.getTotal());
        json.put("dataList", page.getRecords());
        return json;
    }



}
